/*jshint esversion:6 */
import PointerInteraction from 'ol/interaction/Pointer';

function Drag() { //老方法
  //私有属性
  this.coordinate_ = null;
  this.cursor_ = 'pointer';
  this.previousCursor_ = null;
  this.feature_ = null;
  PointerInteraction.call(this, {
    handleDownEvent: handleDownEvent,
    handleDragEvent: handleDragEvent,
    handleMoveEvent: handleMoveEvent,
    handleUpEvent: handleUpEvent
  });

  function handleDownEvent(evt) { //按下鼠标
    var map = evt.map;
    var feature = map.forEachFeatureAtPixel(evt.pixel,
      function (feature) {
        return feature;
      });

    if (feature) {
      this.coordinate_ = evt.coordinate;
      this.feature_ = feature;
    }
    return !!feature;
  }
  /**
   * @param {module:ol/MapBrowserEvent~MapBrowserEvent} evt Map browser event.
   */
  function handleDragEvent(evt) {
    var deltaX = evt.coordinate[0] - this.coordinate_[0];
    var deltaY = evt.coordinate[1] - this.coordinate_[1];

    var geometry = this.feature_.getGeometry();
    geometry.translate(deltaX, deltaY);

    this.coordinate_[0] = evt.coordinate[0];
    this.coordinate_[1] = evt.coordinate[1];
  }
  /**
   * @param {module:ol/MapBrowserEvent~MapBrowserEvent} evt Event.
   */
  function handleMoveEvent(evt) { //更换手型
    if (this.cursor_) {
      var map = evt.map;
      var feature = map.forEachFeatureAtPixel(evt.pixel,
        function (feature) {
          return feature;
        });
      var element = evt.map.getTargetElement();
      if (feature) {
        if (element.style.cursor != this.cursor_) {
          this.previousCursor_ = element.style.cursor;
          element.style.cursor = this.cursor_;
        }
      } else if (this.previousCursor_ !== undefined) {
        element.style.cursor = this.previousCursor_;
        this.previousCursor_ = undefined;
      }
    }
  }
  /**
   * @return {boolean} `false` to stop the drag sequence.
   */
  function handleUpEvent() {
    this.coordinate_ = null;
    this.feature_ = null;
    return false;
  }
}

//继承关系
Drag.__proto__ = PointerInteraction;
Drag.prototype = Object.create(PointerInteraction.prototype); //指向一致的原型
Drag.prototype.construct = Drag;


//导出
export default Drag;